package com.example.echoewish.repository.network;

import com.example.echoewish.pojo.Post;
import com.example.echoewish.repository.network.service.PostService;
import com.example.echoewish.util.ServiceCreator;

import java.io.File;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Callback;


public class PostServiceNetwork {

    public static void publishPost(File image, Post post, Callback callback)
    {
        PostService postService =new ServiceCreator().create(PostService.class);
        RequestBody body=null;
        if(image!=null)
        {

            MultipartBody.Builder builder = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("file",post.postImage,RequestBody.create(image,MediaType.parse("image/*")))
                    .addFormDataPart("filename", post.postImage)
                    .addFormDataPart("user_id", String.valueOf(post.userId))
                    .addFormDataPart("title",post.title)
                    .addFormDataPart("zone",post.zone)
                    .addFormDataPart("content",post.content)
                    .addFormDataPart("time",post.time);
            body=builder.build();
        }
        else
        {
            FormBody.Builder builder=new FormBody.Builder()
                    .add("user_id",String.valueOf(post.userId))
                    .add("title",post.title)
                    .add("zone",post.zone)
                    .add("content",post.content)
                    .add("time",post.time)
                    .add("reward",post.reward);
            body=builder.build();
        }
        postService.publishPost(body).enqueue(callback);
    }

    public static void deletePost(int userId,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.deletePost(userId).enqueue(callback);
    }

    public static void queryPost(int postId,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryPost(postId).enqueue(callback);
    }

    public static void queryDetailPost(int userId,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryDetailPost(userId).enqueue(callback);
    }

    public static void queryPartPost(int start,int end,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryPartPost(start,end).enqueue(callback);
    }

    public static void queryPartPostByZone(int start,int end,String zone,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryPartPostByZone(start,end,zone).enqueue(callback);
    }

    public static void  queryPartPostByKeyWord(String keyword,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryPartPostByKeyWord(keyword).enqueue(callback);
    }

    public static void  queryPartPostByZoneAndKeyWord(String zone,String keyword,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryPartPostByZoneAndKeyWord(zone,keyword).enqueue(callback);
    }

    public static void queryMyPublishPostByUserId(int userId,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.queryMyPublishPostByUserId(userId).enqueue(callback);
    }

    public static void getHotSearch(int num,Callback callback)
    {
        PostService postService=new ServiceCreator().create(PostService.class);
        postService.getHotSearch(num).enqueue(callback);
    }
}
